﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Way.Lib;

namespace CheckIn.ToolsHost.Infrastructures
{
    public class StrokeCountService
    {

        Dictionary<string, int> _dict = new Dictionary<string, int>();
        public StrokeCountService()
        {
            var arrs = File.ReadAllText(@"StrokeDict.txt", Encoding.UTF8).Split('\n').Select(m=>m.Trim()).Where(m=>m.Length > 0).Select(m=>m.Split(','));
            foreach( var itemArr in arrs )
            {
                if (string.IsNullOrEmpty(itemArr[2]))
                    itemArr[2] = itemArr[1];
                int strokecount = Convert.ToInt32(itemArr[2]);
                _dict[itemArr[0]] = strokecount;
            }
        }

        /// <summary>
        /// 获取文字的笔画
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        public int StrokeCount(string word)
        {
            if (word.Length > 1)
                word = word.Substring(0, 1);

            if (_dict.TryGetValue(word, out int v))
                return v;

            return 0;
        }
    }
}
